<template>
  <div>
    <h3 style="text-align: center;">供应商管理</h3>
    <div style="height:250px">
      <el-table :data="cutObj.records" style="width: 100%" border stripe>
        <el-table-column prop="name" label="供应商名称"/>
        <el-table-column prop="headerName" label="负责人"/>
        <el-table-column prop="ranges" label="经营范围"/>
        <el-table-column fixed="right" label="操作">
          <template #default="scope">
            <el-button type="danger" size="small" @click="findById(scope.row.id)">查看</el-button>
            <el-button type="primary" size="small" @click="findByUpdateId(scope.row.id)">修改信息</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div style="margin: 20px">
      <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="cutObj.size"
          background
          layout="prev, pager, next, jumper"
          :total="cutObj.total"
          @current-change="findByItem"
      />
    </div>
    供应商Excel：<input type="file" id="myFile">
    <input type="button" value="添加" @click="upFile">

    <div style="display: flex;justify-content: space-around">
      <el-button type="primary" @click="showAdd = true">添加供应商</el-button>
      <div>
        供应商名称：
        <el-input v-model="name" style="width: 200px"/>
        <br>
        经营范围：
        <el-input v-model="ranges" style="width: 200px"/>
        <br>
        <el-button type="primary" @click="findByItem(1)">查询</el-button>
      </div>
    </div>

    <!--    查询用户弹出框-->
    <el-dialog
        v-model="showId"
        title="添加用户"
        width="40%">
      供应商名称：{{ vendorObj.name }}<br>
      负责人：{{vendorObj.headerName}}<br>
      负责人电话：{{vendorObj.headerPhone}}<br>
      供应商地址：{{vendorObj.address}}<br>
      经营范围：{{vendorObj.ranges}}
      <template #footer>
    <span class="dialog-footer">
    <el-button @click="showId = false">取消</el-button>
    <el-button type="primary" @click="showId = false">确定</el-button>
  </span>
      </template>
    </el-dialog>

    <!--    修改用户弹出框-->
    <el-dialog
        v-model="showUpdate"
        title="添加用户"
        width="40%">
      供应商名称：{{ vendorObj.name }}<br>
      负责人：<el-input v-model="vendorObj.headerName" style="width: 200px"/><br>
      负责人电话：<el-input v-model="vendorObj.headerPhone" style="width: 200px"/><br>
      供应商地址：{{vendorObj.address}}<br>
      经营范围：{{vendorObj.ranges}}
      <template #footer>
    <span class="dialog-footer">
    <el-button @click="showUpdate = false">取消</el-button>
    <el-button type="primary" @click="findByUpdate">确定</el-button>
  </span>
      </template>
    </el-dialog>

    <!--    添加用户弹出框-->
    <el-dialog
        v-model="showAdd"
        title="添加用户"
        width="40%">
      供应商名称：<el-input v-model="vendorObj.name" style="width: 200px"/><span>{{content}}</span> <br>
      负责人：<el-input v-model="vendorObj.headerName" style="width: 200px"/><br>
      负责人电话：<el-input v-model="vendorObj.headerPhone" style="width: 200px"/><br>
      供应商地址：<el-input v-model="vendorObj.address" style="width: 200px"/> <br>
      经营范围：<el-input v-model="vendorObj.ranges" style="width: 200px"/>
      <template #footer>
    <span class="dialog-footer">
    <el-button @click="showAdd = false">取消</el-button>
    <el-button type="primary" @click="add">确定</el-button>
  </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      showAdd:false,
      showUpdate:false,
      cutObj: {},
      name: "",
      ranges: "",
      currentPage: 1,
      userObj: {},
      showId: false,
      vendorObj:{},
      content:"",
    }
  }, methods: {
    findByItem(pageNO) {
      this.currentPage = pageNO;
      axios.get("/project/vendor/findByItem", {
        params: {
          pageNO,
          name: this.name,
          ranges: this.ranges
        }
      }).then(resp => {
        this.cutObj = resp.data;

      })
    },
    findById(vendorId) {
      this.showId = true;
      axios.get("/project/vendor/findById",{
        params:{vendorId}
      }).then(resp=>{
        this.vendorObj= resp.data
      })
    },
    findByUpdateId(vendorId){
      this.showUpdate = true;
      axios.get("/project/vendor/findById",{
        params:{vendorId}
      }).then(resp=>{
        this.vendorObj= resp.data
      })
    },
    findByUpdate(){
      this.showUpdate = false;
      axios.get("/project/vendor/updateVendor",{
        params:{
          vendorId:this.vendorObj.id,
          headerName:this.vendorObj.headerName,
          headerPhone:this.vendorObj.headerPhone
        }
      });
      this.findByItem(1)
    },
    add(){
      axios.get("/project/vendor/addVendor",{
        params: this.vendorObj
      }).then(resp =>{
        if (resp.data == "ok"){
          this.showAdd = false;
          this.findByItem(1)
        }
      })
    },
    upFile(){
      var formObj = new FormData;
      formObj.append("excelFile",document.getElementById("myFile").files[0]);
      let config = {
        headers:{'Content-Type':'multipart/form-data'}
      }
      axios.post("/project/excel/add",formObj,config).then(resp =>{
        if (resp.data == "ok"){
          alert("添加成功");
          // this.cutByItem(1)
        }
      })

    },
  },
  created() {
    this.userObj = JSON.parse(sessionStorage.getItem("loginUser"));
    this.findByItem(1);
  }
}
</script>

<style scoped>

</style>